package com.keller.dao;

import com.keller.domian.Permission;
import com.keller.domian.Role;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface RoleDao {

    @Select("select * from role where id in (select roleid from users_role where userid=#{userId})")
    @Results({
            @Result(id = true, property = "id", column = "id"),
            @Result(property = "roleName", column = "roleName"),
            @Result(property = "roleDesc", column = "roleDesc"),
            @Result(property = "permissions", column = "id", many = @Many(select = "com.keller.dao.PermissionDao.findPermissionByRoleId"))
    })
    public List<Role> findRoleById(String userId);

    @Select("select * from role")
    public List<Role> findAllRoles();

    @Insert("insert into role (roleName,roleDesc) values (#{roleName},#{roleDesc})")
    public void saveRole(Role role);

    //根据角色id查询角色
    @Select("select * from role where id=#{id}")
    public Role findRoleByRoleId(String id);

    //根据角色id查询角色没有的权限
    @Select("select * from permission where id not in (select permissionid from role_permission where roleid=#{id})")
    public List<Permission> findOtherPermissionByRoleId(String id);

    //将用户添加到角色,不加@Param()注解会报错，不知道为啥。
    @Insert("insert into role_permission (roleid, permissionid) values (#{roleId}, #{permissionId})")
    public void addPermissionToRole(@Param("roleId")String roleId, @Param("permissionId") String permissionId);
}
